[Day 6] Statistic: 來呈現文章瀏覽數逐漸下滑的趨勢(>_<)
今天我們來實作Day #5 搭配上筆者Thibe這一週的文章瀏覽和預期數據,看來這下滑趨勢達到顯著呢(p < 0.05)!

此次作品建議開啟CodePen觀看。
CodePen: https://codepen.io/stevetanus/pen/XWqjMbV
1. HTML(架構圖)


抱歉我寫字寫得很醜~
.card之下,由上到下分成四個區塊,.header包含四個文字區塊,.legend包含兩個文字區塊,比較難的地方是.statistic,裡面用到了SVG畫線、pseudo-element偽元素(::before, ::after)、hover效果顯示數值,days包 含了七個日期。
SVG
<div class="data red">
<svg>
<polyline points="9,1 50,16 90,20 130,25 171,30 211,35 251,40"></polyline>
</svg>
<div class="points">
<div class="point-1">
<div class="tooltip">200</div>
</div>
<div class="point-2">
<div class="tooltip">160</div>
</div>
<div class="point-3">
<div class="tooltip">150</div>
</div>
<div class="point-4">
<div class="tooltip">145</div>
</div>
<div class="point-5">
<div class="tooltip">130</div>
</div>
<div class="point-6">
<div class="tooltip">115</div>
</div>
<div class="point-7">
<div class="tooltip">100</div>
</div>
</div>
上面呈現在.statistic中的紅線,SVG的polyline屬性會將點用直線連起來,這邊定義了七個點,在points裡面,有著七點的tooltip數值,之後會透過CSS顯示在數據線上。
2. SCSS(CSS)
.header
.header {
position: relative;
background: #f1ba64;
width: 100%;
height: 60px;
padding: 2px;
.big {
position: absolute;
font-weight: 600;
font-size: 14px;
text-transform: uppercase;
}
.small {
position: absolute;
font-weight: 400;
font-size: 12px;
}
...
}
在.header中,比較大的文字套用.big,比較小的文字套用.small,四個span都是用絕對定位。
.legend
.legend {
text-align: right;
padding: 10px 0 14px 0;
span {
position: relative;
...
padding: 0 10px 0 25px;
}
span::before {
position: absolute;
left: 6px;
top: 6px;
display: block;
content: "";
width: 11px;
height: 3px;
border-radius: 3px;
}
.red::before {
background: $red;
}
.blue::before {
background: $blue;
}
}
.legend中使用了text-align: right;(tar)置右,兩個span都為相對定位點,有著右邊的padding: 10px,左邊的padding: 25px。在span::before(偽元素)為絕對定位,要注意的是在偽元素(::before,::after等等)內要寫入content屬性才會生效,.red跟.blue的::before偽元素分別為紅色與藍色的短直線。
.statistic
.line1, .line2, .line3
.statistic {
...
.line1 {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 1px;
background: #f2f2f2;
}
.line2 {
@extend .line1;
top: 40px;
}
.line3 {
@extend .line1;
top: inherit;
bottom: 0;
}
.line1、.line2、.line3,皆為1px的直線,其中@extend .line1讓後面兩條線繼承.line1的屬性。
polyline
polyline {
fill: none;
stroke-width: 2;
stroke-linecap: round;
}
&.red polyline {
stroke: $red;
}
&.blue polyline {
stroke: $blue;
}
polyline的fill要設為none,寬度為2px,邊框端點設為round,.red設為紅色,.blue設為藍色。